﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command;
using B.S.DataBase.MyRead.API.DTOs;
using MediatR;
using System;
using static Humanizer.In;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler
{
    public class GetUserRoleCommandHandler : IRequestHandler<GetUserRoleCommand, APIResult<List<UserRoleDTO>>>
    {
        private readonly IBaseRepository<User> userRep;

        private readonly IBaseRepository<Role> roleRep;

        private readonly IBaseRepository<RoleUser> ruRep;

        public GetUserRoleCommandHandler(IBaseRepository<User> userRep, IBaseRepository<Role> roleRep, IBaseRepository<RoleUser> ruRep)
        {
            this.userRep = userRep;
            this.roleRep = roleRep;
            this.ruRep = ruRep;
        }

        public Task<APIResult<List<UserRoleDTO>>> Handle(GetUserRoleCommand request, CancellationToken cancellationToken)
        {
            APIResult<List<UserRoleDTO>> result = new APIResult<List<UserRoleDTO>>();

            var list = from a in userRep.GetAll()
                       join b in ruRep.GetAll() on a.UserId equals b.UserId
                       join o in roleRep.GetAll() on b.RoleId equals o.RoleId
                       where o.RoleId == request.RoleId&&b.IsDelete==false
                       select new UserRoleDTO
                       {
                           UserId=a.UserId,
                           UserName=a.UserName,
                           DepId=a.DepId,
                           NickName=a.NickName,
                           Sex=a.Sex,
                           ProvinceId = a.ProvinceId,
                           CityId=a.CityId,
                           CountyId= a.CountyId,
                           HamletName = a.HamletName,
                           RoleId=o.RoleId,
                           RoleName = o.RoleName,
                           RUId=b.RUId
                       };
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            result.data = list.ToList();
            return Task.FromResult(result);
        }
    }
}
